﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SolutionModel;
using SFast.DB;
using SFast.Web;

namespace SolutionDAL
{
    public class T_AdminRolesDAL
    {

        #region 检查数据库中是否已经存在某角色名称
        /// <summary>
        /// 检查数据库中是否已经存在某角色名称
        /// </summary>
        /// <param name="RolesName">当前需要验证的角色名称</param>
        /// <returns></returns>
        public int HasRolesName(String RolesName, String id)
        {
            PurviewDBEntities db = DBConfig.GetDBContext();
            if (string.IsNullOrEmpty(id))
            {
                return db.T_AdminRoles.Where(o => o.RoleName == RolesName).Count();
            }
            else
            {
                return db.T_AdminRoles.Where(o => o.RoleName == RolesName & o.RoleID != id).Count();
            }

        }
        #endregion

        #region 添加角色
        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="obj"></param>
        public void InsertAdminRoles(T_AdminRole obj)
        {
            PurviewDBEntities db = DBConfig.GetDBContext();
            db.T_AdminRoles.Insert(obj);
        }
        #endregion

        #region 获取角色列表
        /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        public IQueryable<T_AdminRole> GetAdminRolesList()
        {
            PurviewDBEntities db = DBConfig.GetDBContext();
            var q= db.T_AdminRoles.IncludeWith<T_AdminRole>(o => o.T_MenuInRoles).OrderByDescending(o => o.CreateTime);
            return q;
            //return db.T_AdminRoles.Include(()OrderByDescending
        }
        #endregion

        #region 通过IsDisabled 来获取角色，true为启动  false 为禁用
        /// <summary>
        /// 通过IsDisabled 来获取角色，true为启动  false 为禁用
        /// </summary>
        /// <param name="isd"></param>
        /// <returns></returns>
        public List<T_AdminRole> Get_AdminRolesList_By_IsDisabled(bool isd, String key = null)
        {
            PurviewDBEntities db = DBConfig.GetDBContext();
            if (key == null)
            {
                return db.T_AdminRoles.Where(o => o.IsDisabled == isd).ToList<T_AdminRole>();
            }
            else
            {
                return db.T_AdminRoles.Where(o => o.IsDisabled == isd & o.RoleName.Contains(key.ToString())).ToList<T_AdminRole>();
            }

        }
        #endregion

        #region 通过角色ID获取角色信息，返回回填表单的js
        /// <summary>
        /// 通过角色ID获取角色信息，返回回填表单的js
        /// 标记：要修改 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public void GetAdminRolesByIDToJs(String id)
        {
            PurviewDBEntities db = DBConfig.GetDBContext();
            var role = db.T_AdminRoles.Where(o => o.RoleID == id).FirstOrDefault();
            role._DataToForm("form1");
        }
        #endregion

        #region 更新管理员角色
        /// <summary>
        /// 更新管理员角色
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int UpdateAdminRoles(T_AdminRole obj)
        {
            PurviewDBEntities db = DBConfig.GetDBContext();
            var roles = db.T_AdminRoles.FirstOrDefault(o => o.RoleID == obj.RoleID);
            if (roles != null)
            {
                roles.RoleName = obj.RoleName;
                roles.RoleIntro = obj.RoleIntro;
                roles.IsDisabled = obj.IsDisabled;
            }
            return db.T_AdminRoles.Update(roles);
        }
        #endregion
    }
}
